/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import e.odbo.data.dao.ViewDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.bean.ProductLocationState;
import com.openbravo.bean.viewbean.ProductStoreView;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ProductStoreInfoDAO extends ViewDAO<ProductStoreView> {

	private static String LIST_Sql="SELECT P.ID, P.REFERENCE, P.CODE, P.NAME, P.PRICEBUY, P.PRICESELL, L.NAME ,A.DESCRIPTION,C.UNITS,C.INDATE,C.INREASON,C.OUTDATE,C.OUTREASON,PLS.STATE "
        + "FROM STOCKCURRENT C LEFT OUTER JOIN ATTRIBUTESETINSTANCE A ON  C.ATTRIBUTESETINSTANCE_ID=A.ID LEFT OUTER JOIN PRODUCT_LOCATION_STATE PLS ON  C.LOCATION=PLS.LOCATION_ID AND C.PRODUCT=PLS.PRODUCT_ID ,PRODUCTS P ,LOCATIONS L  ";
	
	private static String LIST_AdvSql="SELECT P.ID, P.REFERENCE, P.CODE, P.NAME, P.PRICEBUY, P.PRICESELL, L.NAME ,A.DESCRIPTION,C.UNITS,C.INDATE,C.INREASON,C.OUTDATE,C.OUTREASON,PLS.STATE  "
        + "FROM STOCKCURRENT C LEFT OUTER JOIN ATTRIBUTESETINSTANCE A ON  C.ATTRIBUTESETINSTANCE_ID=A.ID LEFT OUTER JOIN PRODUCT_LOCATION_STATE PLS ON  C.LOCATION=PLS.LOCATION_ID AND C.PRODUCT=PLS.PRODUCT_ID ,PRODUCTS P ,BOMS B,LOCATIONS L  ";
	
    public ProductStoreInfoDAO(I_Session s) {
        super(s);
    }

    public ProductStoreView readValues(DataRead dr,ProductStoreView p) throws BasicException {
        if(p==null)
        	p=new ProductStoreView();
         p.setM_ID( dr.getString(1));
          p.setM_sRef(dr.getString(2));
          p.setM_sCode(dr.getString(3));
          p.setM_sName(dr.getString(4));
          p.setM_dPriceBuy(dr.getDouble(5).doubleValue());
          p.setM_dPriceSell( dr.getDouble(6).doubleValue());
          p.setM_Location(dr.getString(7));
          p.setAttrsetInsDesc(dr.getString(8));
          p.setM_unit(dr.getDouble(9));
          p.setInDate(dr.getTimestamp(10));
          p.setInReason(dr.getString(11));
          p.setOutDate(dr.getTimestamp(12));
          p.setOutReason(dr.getString(13));
          p.setState(dr.getInt(14)==null?ProductLocationState.STATE_INOUT:dr.getInt(14));
          return p;
    }

    public List<ProductStoreView> getSimilarProductStoreQBF(Object filter)  throws BasicException{
        return new StaticSentence(s, LIST_Sql+ "WHERE C.PRODUCT=P.ID AND C.LOCATION=L.ID AND P.ID != ?  AND P.SIMILARCODE = (SELECT SIMILARCODE FROM PRODUCTS WHERE ID = ?) AND P.SIMILARCODE IS NOT NULL AND P.SIMILARCODE!=''", new SerializerWriteBasic(Datas.STRING, Datas.STRING), this).list(filter);
    }

    public List<ProductStoreView> getProductBomIncludeStoreQBF(Object filter)  throws BasicException{
        return new StaticSentence(s, LIST_AdvSql+"WHERE C.PRODUCT=P.ID AND C.LOCATION=L.ID AND P.ID=B.REFID AND B.PARENT=? ", SerializerWriteString.INSTANCE, this).list(filter);

    }

    public List<ProductStoreView> getProductIncludeBomStoreQBF(Object filter)  throws BasicException{
        return new StaticSentence(s, LIST_AdvSql+ "WHERE C.PRODUCT=P.ID AND C.LOCATION=L.ID AND P.ID=B.PARENT AND B.REFID=? ", SerializerWriteString.INSTANCE, this).list(filter);

    }

    public List<ProductStoreView> listProductStoreByProductID(String filter) throws BasicException {
        return new StaticSentence(s, LIST_Sql+ "WHERE C.PRODUCT=P.ID AND C.LOCATION=L.ID AND C.PRODUCT= ?", SerializerWriteString.INSTANCE, this).list(filter);
    }

    public List<ProductStoreView> listByIdAndVirtual(final String id,final boolean isVirtual) throws BasicException {
        return new StaticSentence(s, LIST_Sql+ "WHERE C.PRODUCT=P.ID AND C.LOCATION=L.ID AND C.PRODUCT = ? AND L.ISVIRTUAL = ?", SerializerWriteParams.INSTANCE,this).list(new DataParams(){
			@Override
			public void writeValues() throws BasicException {
				this.setString(1, id);
				this.setBoolean(2, isVirtual);
			}
		});
    }
    
    @Override
    public Class getSuportClass() {
        return ProductStoreView.class;
    }


}
